package com.sinosoft.dsp.component.bpm.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputHidden;
import javax.faces.event.ActionEvent;

import org.jbpm.xsdobject.Process;
import org.jbpm.xsdobject.TransitionType;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.chart.flow.FlowChartModel;
import org.primefaces.model.chart.flow.FlowElementSet;
import org.primefaces.model.chart.flow.FlowHop;
import org.primefaces.model.chart.flow.FlowStep;

import com.sinosoft.dsp.component.bpm.exception.SinoBPMException;
import com.sinosoft.dsp.component.bpm.facade.engine.ProcessResourceFacade;
import com.sinosoft.dsp.component.bpm.util.ActivityType;
import com.sinosoft.dsp.component.bpm.util.DeploymentFileType;
import com.sinosoft.dsp.component.bpm.util.JbpmConfig;
import com.sinosoft.dsp.component.bpm.util.ProcessParse;
import com.sinosoft.dsp.component.bpm.viewbean.BpmAddEditDefineViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.BpmAddEditPackageViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.BpmConfigDataObject;
import com.sinosoft.dsp.component.bpm.viewbean.BpmJpdlDefineViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.IBpmAddEditDefineViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.IBpmAddEditPackageViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.IBpmConfigViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.IBpmActivityEditViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.part.BpmActivityAwokesViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.part.BpmActivityCommonBaseViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.part.BpmActivityFieldsViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.part.BpmActivityFormViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.activity.part.BpmActivityPrivilegesViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.transition.IBpmTransitionEditViewBean;
import com.sinosoft.dsp.component.bpm.viewbean.transition.part.BpmTransitionCommonBaseViewBean;
import com.sinosoft.dsp.platform.manager.ApplicationController;
import com.sinosoft.dsp.platform.utils.FacesContextUtils;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.utils.Const;

/**
 * 平台通用页面行为类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-4-6 下午09:26:56
 */
@ManagedBean(name = "bpmAction")
@RequestScoped
public class BpmAction {

	private static final Log log = Log.getLog(BpmAction.class.getName());

	@ManagedProperty(value = "#{applicationController}")
	private ApplicationController applicationController;

	@ManagedProperty(value = "#{processResourceFacade}")
	private ProcessResourceFacade prf;

	/**
	 * 保存定义活动的行为入口
	 * 
	 * @param actionEvent
	 * @throws SinoBPMException
	 */
	public void saveDefineActivityAction(ActionEvent actionEvent)
			throws SinoBPMException {
		UIComponent menu = actionEvent.getComponent();
		HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren().get(
				0);
		// 获得参数
		String viewBeanId = (String) hiddenInput.getValue();

		IBpmActivityEditViewBean ae = (IBpmActivityEditViewBean) applicationController
				.getViewBeanById(viewBeanId);
		BpmJpdlDefineViewBean jpdl = (BpmJpdlDefineViewBean) ae.getParent();
		org.jbpm.xsdobject.Process process = jpdl.getProcess();

		// 获得活动
		String name = (String) ae
				.getField(BpmActivityCommonBaseViewBean.FIELD_NAME);
		String initName = (String) ae
				.getField(BpmActivityCommonBaseViewBean.FIELD_INIT_NAME);
		Object act = null;
		for (Object obj : process.getDescriptionAndSwimlaneAndOn()) {
			String an = ActivityType.getActivityName(obj);
			if (initName.equals(an)) {
				act = obj;
				break;
			}
		}
		if (act == null) {
			String type = (String) ae
					.getField(BpmActivityCommonBaseViewBean.FIELD_TYPE);
			ActivityType at = ActivityType.getActivityType(type);
			act = at.createObject(name);
		}

		ActivityType.createOrUpdateActivityObject(act, ae);

		// 如果活动名称修改，同时修改转移
		if (!initName.equals(name)) {
			for (Object obj : process.getDescriptionAndSwimlaneAndOn()) {
				List<?> trans = ActivityType.getActivityTransitions(obj);
				if (trans != null) {
					for (Object t : trans) {
						if (((TransitionType) t).getTo().equals(initName)) {
							((TransitionType) t).setTo(name);
						}
					}
				}
			}
		}
	}

	public void saveDefineTransitionAction(ActionEvent actionEvent)
			throws SinoBPMException {
		UIComponent menu = actionEvent.getComponent();
		HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren().get(
				0);
		// 获得参数
		String viewBeanId = (String) hiddenInput.getValue();

		IBpmTransitionEditViewBean te = (IBpmTransitionEditViewBean) applicationController
				.getViewBeanById(viewBeanId);
		BpmJpdlDefineViewBean jpdl = (BpmJpdlDefineViewBean) te.getParent();
		org.jbpm.xsdobject.Process process = jpdl.getProcess();

		// 获得活动
		String fromActivityName = (String) te
				.getField(BpmTransitionCommonBaseViewBean.FIELD_FROM_ACTIVITY_NAME);
		String toActivityName = (String) te
				.getField(BpmTransitionCommonBaseViewBean.FIELD_TO_ACTIVITY_NAME);
		Object act = null;
		for (Object obj : process.getDescriptionAndSwimlaneAndOn()) {
			String an = ActivityType.getActivityName(obj);
			if (fromActivityName.equals(an)) {
				act = obj;
				break;
			}
		}
		String type = (String) te
				.getField(BpmTransitionCommonBaseViewBean.FIELD_FROM_ACTIVITY_TYPE);
		ActivityType at = ActivityType.getActivityType(type);
		if (act == null) {
			act = at.createObject(fromActivityName);
		}

		// 获得转移
		String name = (String) te
				.getField(BpmTransitionCommonBaseViewBean.FIELD_NAME);
		String initName = (String) te
				.getField(BpmTransitionCommonBaseViewBean.FIELD_INIT_NAME);
		TransitionType tt = null;
		for (Object obj : ActivityType.getActivityTransitions(act)) {
			if (((TransitionType) obj).getName().equals(initName)) {
				tt = (TransitionType) obj;
				break;
			}
		}
		if (tt == null) {
			tt = ActivityType.createOrUpdateActivityTransition(act, name,
					toActivityName, "");
		}

		at.createOrUpdateTransisionObject(tt, te);
	}

	/**
	 * 保存jpdl定义，只保存位置信息
	 * 
	 * @param actionEvent
	 * @throws SinoBPMException
	 */
	public void saveJpdlDefineAction(ActionEvent actionEvent)
			throws SinoBPMException {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();

			BpmJpdlDefineViewBean vb = (BpmJpdlDefineViewBean) applicationController
					.getViewBeanById(viewBeanId);

			JSONObject json = new JSONObject(vb.getPageCanvasString());
			JSONObject canvasEls = json
					.getJSONObject(FlowChartModel.CANVAS_ELS);
			Map<String, String[]> p = new HashMap<String, String[]>();

			// 1.节点
			Map<String, Object> activityMap = new HashMap<String, Object>();
			if (canvasEls.has(FlowElementSet.STEPS)) {
				JSONArray steps = canvasEls.getJSONArray(FlowElementSet.STEPS);
				for (int i = 0; i < steps.length(); i++) {
					JSONObject step = (JSONObject) steps.get(i);
					if (step.has(FlowStep.EXTEND_DATA)) {
						JSONObject extendData = step
								.getJSONObject(FlowStep.EXTEND_DATA);
						String id = step.getString(FlowStep.ID);
						String name = extendData.optString("name");
						String type = extendData.optString("type");
						String g = "";
						g += String.valueOf(step.get(FlowStep.D_X));
						g += ",";
						g += String.valueOf(step.get(FlowStep.D_Y));
						g += ",";
						g += String.valueOf(step.get(FlowStep.D_WIDTH));
						g += ",";
						g += String.valueOf(step.get(FlowStep.D_HEIGHT));
						p.put(name, new String[] { g, type, id });
					}
				}
			}

			List<Object> forDel = new ArrayList<Object>();

			org.jbpm.xsdobject.Process process = vb.getProcess();
			for (Object obj : process.getDescriptionAndSwimlaneAndOn()) {
				String[] g = p.get(ActivityType.getActivityName(obj));
				// 删除该节点
				if (g == null) {
					forDel.add(obj);
				} else {
					p.remove(ActivityType.getActivityName(obj));

					ActivityType.setActivityCoordinates(obj, g[0]);

					activityMap.put(g[2], obj);
				}
			}

			for (Object obj : forDel) {
				process.getDescriptionAndSwimlaneAndOn().remove(obj);
			}

			// 添加新节点
			if (p.size() > 0) {
				for (Map.Entry<String, String[]> entry : p.entrySet()) {
					String name = entry.getKey();
					String[] g = entry.getValue();
					ActivityType at = ActivityType.getActivityType(g[1]);
					Object obj = at.createObject(name);
					ActivityType.setActivityCoordinates(obj, g[0]);
					process.getDescriptionAndSwimlaneAndOn().add(obj);

					activityMap.put(g[2], obj);
				}
			}

			// 2.连线
			Map<String, Object> hasHopNameMap = new HashMap<String, Object>();
			if (canvasEls.has(FlowElementSet.HOPS)) {
				JSONArray hops = canvasEls.getJSONArray(FlowElementSet.HOPS);
				for (int i = 0; i < hops.length(); i++) {
					JSONObject hop = (JSONObject) hops.get(i);

					JSONObject extendData = hop
							.getJSONObject(FlowHop.EXTEND_DATA);
					String name = extendData.optString("name");
					hasHopNameMap.put(name, null);

					Object fromActivity = activityMap.get(hop
							.getString(FlowHop.FROM_EL_ID));
					Object toActivity = activityMap.get(hop
							.getString(FlowHop.TO_EL_ID));
					String toActivityName = ActivityType
							.getActivityName(toActivity);

					String g = "";
					JSONArray jax = hop.getJSONArray(FlowHop.X);
					JSONArray jay = hop.getJSONArray(FlowHop.Y);
					for (int j = 0; j < jax.length(); j++) {
						g += ",";
						g += String.valueOf(jax.get(j));
						g += ",";
						g += String.valueOf(jay.get(j));
					}
					if (!"".equals(g)) {
						g = g.substring(1);
					}

					ActivityType.createOrUpdateActivityTransition(fromActivity,
							name, toActivityName, g);
				}
			}

			// 删除多余的连线
			for (Object act : activityMap.values()) {
				List<?> trans = ActivityType.getActivityTransitions(act);
				if (trans != null) {
					Iterator<?> iter = trans.iterator();
					while (iter.hasNext()) {
						TransitionType tt = (TransitionType) iter.next();
						if (!hasHopNameMap.containsKey(tt.getName())) {
							iter.remove();
						}
					}
				}
			}

			// 保存
			String text = ProcessParse.objectToXmlOfProcess(process);
			prf.createOrUpdateDeploymentResource(vb.getDeploymentId(), vb
					.getResourceName(), text);

		} catch (Exception e) {
			throw new SinoBPMException("保存jpdl定义出现错误。");
		}
	}

	/**
	 * 保存定义的行为入口
	 * 
	 * @param actionEvent
	 * @throws SinoBPMException
	 */
	public void saveDefineAction(ActionEvent actionEvent)
			throws SinoBPMException {
		UIComponent menu = actionEvent.getComponent();
		HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren().get(
				0);
		// 获得参数
		String viewBeanId = (String) hiddenInput.getValue();

		IBpmAddEditDefineViewBean vb = (IBpmAddEditDefineViewBean) applicationController
				.getViewBeanById(viewBeanId);

		String deploymentId = (String) vb
				.getField(BpmAddEditDefineViewBean.FIELD_DEPLOYMENT_ID);

		// 1.原有的资源名称
		String initResourceName = (String) vb
				.getField(BpmAddEditDefineViewBean.FIELD_INIT_RESOURCE);

		// 2.新的资源名称
		String resourceName = (String) vb
				.getField(BpmAddEditDefineViewBean.FIELD_PARENT_RESOURCE);
		resourceName += (String) vb
				.getField(BpmAddEditDefineViewBean.FIELD_DEFINE_NAME);
		if (!Const.isEmpty(resourceName)
				&& !resourceName.toLowerCase().endsWith(".jpdl.xml")) {
			resourceName += ".jpdl.xml";
		}

		// 3.获得流程定义对象
		org.jbpm.xsdobject.Process process;
		// 获得流程定义
		if (!Const.isEmpty(initResourceName)) {
			JbpmConfig jbpmConfig = SinoManagedBean.findBean("jbpmConfig");

			InputStream inputStream = jbpmConfig.getRepositoryService()
					.getResourceAsStream(deploymentId, resourceName);
			process = ProcessParse.xmlToObjectOfProcess(inputStream);
			try {
				inputStream.close();
			} catch (IOException e) {
				log.debug("无法关闭流");
			}
		}
		// 创建新的流程定义
		else {
			process = new Process();
			process.setName((String) vb
					.getField(BpmAddEditDefineViewBean.FIELD_DEFINE_NAME));
			process.setVersion(0);
		}

		// 设置附加值
		process.getOtherAttributes().clear();
		process.getOtherAttributes().putAll(vb.toMap());

		// 4.删除旧的流程定义
		if (!Const.isEmpty(initResourceName)) {
			prf.deleteDeploymentResource(deploymentId, initResourceName);
		}

		// 5.添加更新流程定义
		String text = ProcessParse.objectToXmlOfProcess(process);
		prf.createOrUpdateDeploymentResource(deploymentId, resourceName, text);

		// 6.更新流程配置页面
		IBpmConfigViewBean config = (IBpmConfigViewBean) vb.getParent();
		config.setReloadFlag();
	}

	/**
	 * 保存流程包
	 * 
	 * @param actionEvent
	 * @throws SinoBPMException
	 */
	public void saveDirAction(ActionEvent actionEvent) throws SinoBPMException {
		UIComponent menu = actionEvent.getComponent();
		HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren().get(
				0);
		// 获得参数
		String viewBeanId = (String) hiddenInput.getValue();

		IBpmAddEditPackageViewBean vb = (IBpmAddEditPackageViewBean) applicationController
				.getViewBeanById(viewBeanId);

		String deploymentId = (String) vb
				.getField(BpmAddEditPackageViewBean.FIELD_DEPLOYMENT_ID);

		// 原始资源名
		String initResourceName = (String) vb
				.getField(BpmAddEditPackageViewBean.FIELD_INIT_RESOURCE);

		// 新资源名
		String resourceName = (String) vb
				.getField(BpmAddEditPackageViewBean.FIELD_PARENT_RESOURCE);
		resourceName += (String) vb
				.getField(BpmAddEditPackageViewBean.FIELD_PACKAGE_NAME);
		resourceName += "/";

		// 0.
		// update
		if (!Const.isEmpty(initResourceName)) {
			if (!initResourceName.equals(resourceName)) {
				// 判断新资源名称是否重名
				if (prf.checkDuplicateResourceName(deploymentId, resourceName)) {
					FacesContextUtils.addErrorMessage("资源名称重复！");
					return;
				}
			}
		}
		// insert
		else {
			// 判断新资源名称是否重名
			if (prf.checkDuplicateResourceName(deploymentId, resourceName)) {
				FacesContextUtils.addErrorMessage("资源名称重复！");
				return;
			}
		}

		// 1.删除原有的包
		if (!Const.isEmpty(initResourceName)) {
			prf.deleteDeploymentResource(deploymentId, initResourceName);
		}

		// 2.添加修改包
		String text = vb.toJson().toString();
		prf.createOrUpdateDeploymentResource(deploymentId, resourceName, text);

		// 3.修改子包和流程定义的名称
		if (!Const.isEmpty(initResourceName)
				&& !initResourceName.equals(resourceName)) {
			prf.renameDeploymentResourceName(deploymentId, initResourceName,
					resourceName);
		}

		// 4.更新流程配置页面
		IBpmConfigViewBean config = (IBpmConfigViewBean) vb.getParent();
		config.setReloadFlag();
	}

	/**
	 * 改变当前节点
	 * 
	 * @param actionEvent
	 */
	public void changeCurrentNode(ActionEvent actionEvent) {
		try {
			String extParams = FacesContextUtils.getParameter("extParams");
			String[] extParamsArr = extParams.split(",");
			IBpmConfigViewBean config = (IBpmConfigViewBean) applicationController
					.getViewBeanById(extParamsArr[0]);
			config.changeCurrentNode(extParamsArr[1]);
		} catch (Exception e) {
			log.error("改变当前节点操作出现错误");
		}
	}

	/**
	 * 删除选中节点
	 * 
	 * @param actionEvent
	 */
	public void deleteSelectedNode(ActionEvent actionEvent) {
		try {
			String extParams = FacesContextUtils.getParameter("extParams");
			String[] extParamsArr = extParams.split(",");
			IBpmConfigViewBean config = (IBpmConfigViewBean) applicationController
					.getViewBeanById(extParamsArr[0]);
			DefaultTreeNode node = config.getNode(extParamsArr[1]);
			BpmConfigDataObject data = (BpmConfigDataObject) node.getData();

			DeploymentFileType fileType = data.getFileType();
			// 删除资源文件
			if (fileType.isFile()) {
				prf.deleteDeploymentResource(data.getDeploymentId(), data
						.getResourceName());
			}
			// 删除资源目录
			else if (fileType == DeploymentFileType.RESOURCE_DIRECTORY) {
				String re = data.getResourceName();
				if (!Const.isEmpty(re)) {
					prf.deleteDeploymentResource(data.getDeploymentId(), re);
				}
			}
			// 删除部署
			else if (fileType == DeploymentFileType.DEPLOYMENT) {
				prf.deleteDeployment(data.getDeploymentId());
			}

			config.setReloadFlag();

		} catch (Exception e) {
			log.error("删除选中节点操作出现错误");
		}
	}

	public ApplicationController getApplicationController() {
		return applicationController;
	}

	public void setApplicationController(
			ApplicationController applicationController) {
		this.applicationController = applicationController;
	}

	public ProcessResourceFacade getPrf() {
		return prf;
	}

	public void setPrf(ProcessResourceFacade prf) {
		this.prf = prf;
	}

	/**
	 * 添加提醒策略
	 * 
	 * @param actionEvent
	 */
	public void addAwokeAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmActivityAwokesViewBean vb = (BpmActivityAwokesViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.addAwokes();

		} catch (Exception e) {
			log.error("添加提醒策略出现错误");
		}
	}

	/**
	 * 删除提醒策略
	 * 
	 * @param actionEvent
	 */
	public void removeAwokeAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmActivityAwokesViewBean vb = (BpmActivityAwokesViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.removeAwokes();
		} catch (Exception e) {
			log.error("删除提醒策略出现错误");
		}
	}

	/**
	 * 添加表单属性
	 * 
	 * @param actionEvent
	 */
	public void addFormElementAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmActivityFormViewBean vb = (BpmActivityFormViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.addFormElement();

		} catch (Exception e) {
			log.error("添加表单属性出现错误");
		}
	}

	/**
	 * 删除表单属性
	 * 
	 * @param actionEvent
	 */
	public void removeFormElementAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmActivityFormViewBean vb = (BpmActivityFormViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.removeFormElement();
		} catch (Exception e) {
			log.error("删除表单属性出现错误");
		}
	}

	/**
	 * 添加参数字段
	 * 
	 * @param actionEvent
	 */
	public void addFieldAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmActivityFieldsViewBean vb = (BpmActivityFieldsViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.addActivityField();

		} catch (Exception e) {
			log.error("添加参数字段出现错误");
		}
	}

	/**
	 * 删除参数字段
	 * 
	 * @param actionEvent
	 */
	public void removeFieldAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmActivityFieldsViewBean vb = (BpmActivityFieldsViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.removeActivityField();
		} catch (Exception e) {
			log.error("删除参数字段出现错误");
		}
	}

	/**
	 * 添加定义参数字段
	 * 
	 * @param actionEvent
	 */
	public void addDefineFieldAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmAddEditDefineViewBean vb = (BpmAddEditDefineViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.addDefineField();

		} catch (Exception e) {
			log.error("添加定义参数字段出现错误");
		}
	}

	/**
	 * 删除定义参数字段
	 * 
	 * @param actionEvent
	 */
	public void removeDefineFieldAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			BpmAddEditDefineViewBean vb = (BpmAddEditDefineViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.removeDefineField();
		} catch (Exception e) {
			log.error("删除定义参数字段出现错误");
		}
	}

	/**
	 * 选择权限操作
	 * 
	 * @param actionEvent
	 */
	public void privilegeAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			HtmlInputHidden typeInput = (HtmlInputHidden) menu.getChildren()
					.get(1);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			String type = (String) typeInput.getValue();
			BpmActivityPrivilegesViewBean vb = (BpmActivityPrivilegesViewBean) applicationController
					.getViewBeanById(viewBeanId);
			vb.action(type);
		} catch (Exception e) {
			log.error("选择权限操作出现错误");
		}
	}

	/**
	 * 流程定义选择权限操作
	 * 
	 * @param actionEvent
	 */
	public void definePrivilegeAction(ActionEvent actionEvent) {
		try {
			UIComponent menu = actionEvent.getComponent();
			HtmlInputHidden hiddenInput = (HtmlInputHidden) menu.getChildren()
					.get(0);
			HtmlInputHidden typeInput = (HtmlInputHidden) menu.getChildren()
					.get(1);
			HtmlInputHidden methodInput = (HtmlInputHidden) menu.getChildren()
					.get(2);
			// 获得参数
			String viewBeanId = (String) hiddenInput.getValue();
			String type = (String) typeInput.getValue();
			String method = (String) methodInput.getValue();
			BpmAddEditDefineViewBean vb = (BpmAddEditDefineViewBean) applicationController
					.getViewBeanById(viewBeanId);
			if ("create".equals(method)) {
				vb.getCreatePrivilegeModel().action(type);
			} else if ("manage".equals(method)) {
				vb.getManagePrivilegeModel().action(type);
			} else if ("view".equals(method)) {
				vb.getViewPrivilegeModel().action(type);
			}
		} catch (Exception e) {
			log.error("流程定义选择权限操作出现错误");
		}
	}
}
